<template>
	<view class="profile">
		<view class="profile_bar" :style="{  height: statusBarHeight + titleBarHeight + 'px' }">
			<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
			<view @click="balck()" class="bar-black" :style="{ height: titleBarHeight + 'px' }">
				<view :style="[blackStyle]"><u-icon name="arrow-left" size="40"></u-icon></view>
			</view>
		</view>
		<view class="profile_content">
			<view class="content-base">
				<view>
					<image src="https://csg-icon.oss-cn-hangzhou.aliyuncs.com/user-index.jpeg" style="width: 750rpx;height: 370rpx;" mode=""></image>
				</view>
				<view class="base-avatar">
					<image style="width: 150rpx; height: 150rpx;border-radius: 50%;border: 2px solid white;" :src="userInfo.avatar" mode="aspectFill"></image>
				</view>
				<view class="base-nickname">
					<view class="nickname-text">{{userInfo.nickname}}</view>
					<view class="nickname-sex">
						<image src="../../../static/img/nan.png" v-if="userInfo.sex == 1" style="width: 40rpx;height: 40rpx;margin-left: 15rpx;" mode=""></image>
						<image src="../../../static/img/nv.png" v-if="userInfo.sex == 0" style="width: 40rpx;height: 40rpx;margin-left: 15rpx;" mode=""></image>
					</view>
				</view>
				<view class="base-sign">{{userInfo.sign}}</view>
				<view class="base-operation">
					<view v-if="userInfo.isFollow === -1" class="operation-btn1" @click="profileData()">编辑资料</view>
					<view v-else class="operation-mine">
						<view @click="enterChat(userInfo.uid, 'private')"><image style="width: 48rpx; height: 48rpx;" src="@/static/user/me.png"></image></view>
						<view v-if="userInfo.isFollow === 0" class="mine-btn" @click="followClick()">关注</view>
						<view v-else class="mine-btn2" @click="unFollowClick()">取消关注</view>
					</view>
				</view>
			</view>
			<view class="content-message">
				<view class="message-item">
					<view class="item-num">{{userMessage.follow}}</view>
					<view class="item-text">关注</view>
				</view>
				<view class="message-line"></view>
				<view class="message-item">
					<view class="item-num">{{userMessage.fans}}</view>
					<view class="item-text">粉丝</view>
				</view>
				<view class="message-line"></view>
				<view class="message-item">
					<view class="item-num">{{userMessage.suck}}</view>
					<view class="item-text">吸宠</view>
				</view>
				<view class="message-line"></view>
				<view class="message-item">
					<view class="item-num">{{userMessage.suck}}</view>
					<view class="item-text">动态</view>
				</view>
			</view>
			<view class="content-list">
				<view class="list-bar">
					<view v-for="(item, index) in tabBar"
						  :key="index" 
						  :class="{'active': currentIndex === index}" 
						  class="bar-item"
						  @click="tabClick(index)">
						{{item}}
					</view>
				</view>
				<scroll-view scroll-y="true"
							 class="scroll-Y"
							 @scrolltolower="toLower">
					<view class="list-item" v-if="currentIndex === 0">
						<view class="item" v-for="item in dyList" :key="item.id">
							<view><image style="width: 72rpx;height: 72rpx;border-radius: 100%;" :src="userInfo.avatar" mode="aspectFill"></image></view>
							<view class="item-info">
								<view class="info-head">
									<view>{{userInfo.nickname}}</view>
									<view class="head-gray" v-if="item.type === 1">添加了宠物</view>
									<view class="head-gray" v-else>发布了领养</view>
								</view>
								<view class="item-time">{{item.createdAt}}</view>
								<view class="item-content" @click="petClick(item.typeId, item.type)">
									<view><image style="width: 56rpx;height: 56rpx;border-radius: 1000%;" :src="item.avatar" mode="aspectFill"></image></view>
									<view class="content-text">{{item.intro}}</view>
								</view>
							</view>
						</view>
						<view class="adopt-empty" v-if="dyList.length === 0">
							<image style="width: 400rpx;height: 280rpx;" src="@/static/user/none1.png" mode="aspectFill"></image>
							<view class="empty-text">暂无数据~</view>
						</view>
					</view>
					<view class="pet-item" v-else>
						<view class="item" v-for="petItem in petList" :key="petItem.pid" @click="myPetClick(petItem.pid)">
							<view><image style="width: 104rpx;height: 104rpx;border-radius: 100%;" :src="petItem.avatar" mode="aspectFill"></image></view>
							<view class="item-info">
								<view style="display: flex; align-items: center;">
									<view class="info-nickname">{{petItem.name}}</view>
									<view class="info-base">
										<view v-if="petItem.sex === 1" class="base-sex" style="background-color: rgba(0, 149, 255, 0.08);">
											<u-icon name="man" color="#0095FF" size="25"></u-icon>
										</view>
										<view v-if="petItem.sex === 0" class="base-sex" style="background-color: rgba(255, 128, 128, 0.08);">
											<u-icon name="woman" color="#FF8080" size="25"></u-icon>
										</view>
										<view class="base-variety">{{petItem.typeName}}</view>
										<view class="base-age">{{ petItem.age ? petItem.age : 0 }}岁</view>
									</view>
								</view>
								<view class="info-sign">{{ petItem.sign ? petItem.sign : '这小家伙很懒，什么都没留下' }}</view>
							</view>
							<view class="item-icon"><image style="width: 44rpx;height: 44rpx;border-radius: 100;" src="@/static/user/more.png"></image></view>
						</view>
						<view class="adopt-empty" v-if="petList.length === 0">
							<image style="width: 400rpx;height: 280rpx;" src="@/static/user/none1.png" mode="aspectFill"></image>
							<view class="empty-text">暂无数据~</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	let systemInfo = uni.getSystemInfoSync();
	let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
	export default {
		data () {
			return {
				tabBar: ['动态', '宠物'],
				currentIndex: 0,
				statusBarHeight: 0,
				titleBarHeight: 0,
				// sexName: 'woman',
				userInfo: {},
				petList: [],
				uid: '',
				userMessage: {},
				currentPage: 1,
				pageSize: 10,
				total: 0,
				dyList: []
			}
		},
		onUnload() {
			if(this.Follows != this.Follow){
				this.$request(this.commonApi.onFollow+this.dataList.pid, 'GET').then(res => {
					// uni.showToast({
					// 	title: '已取消关注',
					// 	duration: 1500,
					// 	icon: 'none'
					// });
					this.getPetData()
					this.getDiaryHome()
				});
			}
		},
		methods: {
			enterChat (uuid, type) {//进入私聊
				if(uni.getStorageSync('login')){
					uni.setStorageSync('objUuid',uuid)
					let self = this
					let data = uni.getStorageSync('masterList')
					let dataLists = JSON.parse(data)
					dataLists.forEach(item=>{
						if(item.uuid == self.userInfo.uid){
							let path = type === self.GoEasy.IM_SCENE.PRIVATE
							// ? '../../../pages/chat/privateChat/privateChat?to=' + self.masterList[1].uuid
							// : '../../../pages/chat/privateChat/privateChat?to=' + self.masterList[1].uuid;
							? '../../../pages/chat/privateChat/privateChat?to=' + item.uuid
							: '../../../pages/chat/privateChat/privateChat?to=' + item.uuid;
							uni.navigateTo({
								url: path
							})
						}
					})
				}else{
					uni.navigateTo({
						url: '../../user/login/index'
					})
				}
				
				// console.log(uuid)
				// uni.navigateTo({
				// 	url: '../../../pages/chat/privateChat/privateChat?to=' + '3bb179af-bcc5-4fe0-9dac-c05688484649'
				// })
			},
			// 顶部导航返回按钮
			balck () {
				uni.navigateBack({
					url:'../index'
				})
			},
			// tab点击
			tabClick (index) {
				this.currentIndex = index
			},
			// 用户资料编辑
			profileData () {
				uni.navigateTo({
				    url: './sub/profileData'
				});
			},
			// 宠物头像跳转主页
			myPetClick (e) {
				console.log(this.dataList)
				/*if(this.dataList.uid == uni.getStorageSync('uid')){
					uni.navigateTo({
						url: '../../../pages/pet/home/home?id='+e+'&type='+1
					})
				}else{
					uni.navigateTo({
						url: '../../../pages/pet/home/home?id='+e+'&type='+2
					})
				}*/
				uni.navigateTo({
					url: '../../../pages/pet/home/home?id=' + e
				})
			},
			// 动态宠物头像跳转主页
			petClick (typeId, type) {
				console.log(typeId, type)
				if (type === 1) {
					uni.navigateTo({
						url: '../../../pages/pet/home/home?id=' + typeId
					})
				} else if (type === 2) {
					uni.navigateTo({
						url: '../../index/details/petDetails?id=' + typeId
					})
				}
			},
			// 关注点击
			followClick () {
				this.$request(this.commonApi.userPay, 'POST', { beUid: this.uid }, 'application/json').then(res => {
					let items = res.data
					if (items.code !== 200) {
						uni.showToast({
							title: items.msg,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '关注成功',
							icon: 'none'
						})
						this.getUserInfo()
					}
				})
			},
			// 取消关注
			unFollowClick () {
				this.$request(this.commonApi.userUnPay, 'POST', { beUid: this.uid }, 'application/json').then(res => {
					let items = res.data
					if (items.code !== 200) {
						uni.showToast({
							title: items.msg,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '已取消关注',
							icon: 'none'
						})
						this.getUserInfo()
					}
				})
			},
			// 获取用户基本信息
			getUserInfo () {
				this.$request(this.commonApi.userInfo + this.uid, 'GET').then(res => {
					let items = res.data.data
					// if (items.sex === 0) {
					// 	this.sexName = 'woman'
					// } else if (items.sex === 1) {
					// 	this.sexName = 'man'
					// } else {
					// 	this.sexName = ''
					// }
					this.userInfo = items
				})
			},
			// 获取我的宠物信息列表
			getMypet () {
				this.$request(this.commonApi.petList + this.uid, 'GET').then(res => {
					let items = res.data
					if (items.code !== 200) {
						uni.showToast({
							title: items.msg,
							icon: 'none'
						})
					} else {
						this.petList = items.data
					}
				})
			},
			// 获取用户统计信息
			getUserMessage () {
				this.$request(this.commonApi.userMessage + this.uid, 'GET').then(res => {
					let items = res.data
					if (items.code !== 200) {
						uni.showToast({
							title: items.msg,
							icon: 'none'
						})
					} else {
						this.userMessage = items.data
					}
				})
			},
			// 获取用户动态
			getUserDy () {
				this.$request(this.commonApi.userDy + this.uid + '/' + this.currentPage + '/' + this.pageSize, 'GET').then(res => {
					let items = res.data
					if (items.code !== 200) {
						uni.showToast({
							title: items.msg,
							icon: 'none'
						})
					} else {
						this.total = items.data.meta.total
						items.data.result.map(item => {
							this.dyList.push(item)
						})
					}
				})
			},
			// 滚动到底部
			toLower (e) {
				console.log(e)
				if (this.currentIndex === 0) {
					if (this.dyList.length < this.total) {
						this.currentPage++
						this.getUserDy()
					} else {
						this.currentPage = 1
						uni.showToast({
							title: '没有数据啦',
							icon: 'none'
						})
					}
				}
			}
		},
		onLoad (option) {
			if (systemInfo.model.indexOf('iPhone') !== -1) {
			  this.titleBarHeight = 44
			} else {
			  this.titleBarHeight = 48
			}
			this.statusBarHeight = systemInfo.statusBarHeight
			
			this.uid = parseInt(option.uid)
		},
		onShow () {
			this.getUserInfo()
			this.getUserMessage()
		},
		mounted () {
			this.getMypet()
			this.getUserDy()
			// 判断用户保存操作，如果用户没有点击，默认为动态
			let currentIndex = uni.getStorageSync('profileIndex')
			if (currentIndex === '') {
				this.currentIndex = 0
			} else {
				this.currentIndex = currentIndex
			}
		},
		computed: {
			// 顶部导航返回样式
			blackStyle () {
				let style = {}
				style.position = 'absolute'
				style.top = (menuButtonInfo.top - this.statusBarHeight) + (menuButtonInfo.height/2) - 10 + 'px'
				style.paddingLeft = 20 + 'rpx'
				return style
			},
			// 性别样式
			sexStyle () {
				let style = {}
				let color = ''
				if (this.userInfo.sex === 0) {
					color = '#FF6582'
				} else if (this.userInfo.sex === 1) {
					color = '#0095FF'
				} else {
					color = ''
				}
				style.backgroundColor = color
				return style
			},
		},
		watch: {
			currentIndex (newValue) {
				uni.setStorageSync('profileIndex', newValue)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.profile {
		height: 100vh;
		.profile_bar {
			position: absolute;
			z-index: 999;
			width: 750rpx;
			.bar-black {
				position: relative;
				width: 750rpx;
			}
		}
		.profile_content {
			.content-base {
				background-color: #fff;
				position: relative;
				.base-avatar {
					height: 160rpx;
					width: 160rpx;
					border-radius: 50%;
					// background-color: #fff;
					// border: 2px solid #F6F6F6;
					position: absolute;
					top: 288rpx;
					left: 30rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
				.base-nickname {
					display: flex;
					align-items: center;
					padding-top: 98rpx;
					padding-left: 30rpx;
					.nickname-text {
						font-size: 36rpx;
						font-weight: 700;
						color: #333333;
						line-height: 50rpx;
					}
					.nickname-sex {
						// width: 36rpx;
						// height: 36rpx;
						border-radius: 50%;
						margin-left: 10rpx;
						display: flex;
						justify-content: space-evenly;
					}
				}
				.base-sign {
					margin-top: 10rpx;
					overflow:hidden;
					text-overflow:ellipsis;
					white-space:nowrap;
					width: 690rpx;
					font-size: 28rpx;
					font-weight: 400;
					color: #999999;
					line-height: 40rpx;
					padding-left: 30rpx;
				}
				.base-operation {
					position: absolute;
					right: 30rpx;
					top: 400rpx;
					.operation-btn1 {
						padding: 0 30rpx;
						height: 60rpx;
						background: #FF9A00;
						border-radius: 8rpx;
						font-size: 28rpx;
						font-weight: 600;
						color: #FFFFFF;
						display: flex;
						align-items: center;
						justify-content: center;
					}
					.operation-mine {
						display: flex;
						.mine-btn {
							padding: 0 30rpx;
							height: 48rpx;
							background: #FF9A00;
							border-radius: 8rpx;
							font-size: 28rpx;
							font-weight: 600;
							color: #FFFFFF;
							line-height: 48rpx;
							text-align: center;
							margin-left: 30rpx;
						}
						.mine-btn2 {
							padding: 0 30rpx;
							height: 48rpx;
							background: #f2f2f2;
							border-radius: 8rpx;
							font-size: 28rpx;
							font-weight: 600;
							line-height: 48rpx;
							text-align: center;
							margin-left: 30rpx;
						}
					}
				}
			}
			.content-message {
				padding: 36rpx 66rpx;
				background-color: #fff;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.message-item {
					text-align: center;
					.item-num {
						font-size: 32rpx;
						font-weight: 500;
						color: #333333;
						line-height: 44rpx;
					}
					.item-text {
						font-size: 28rpx;
						font-weight: 400;
						color: #A3A3A3;
						line-height: 40rpx;
						letter-spacing: 2rpx;
					}
				}
				.message-line {
					width: 2rpx;
					height: 60rpx;
					background: #EEEEEE;
					border-radius: 4rpx;
				}
			}
			.content-list {
				padding: 0 30rpx;
				.scroll-Y {
					height: calc(100vh - 834rpx);
					// height: calc(100vh - 1200rpx);
				}
				.list-bar {
					border-top: 1px solid #F6F6F6;
					border-bottom: 1px solid #F6F6F6;
					display: flex;
					justify-content: space-evenly;
					font-size: 28rpx;
					font-weight: 500;
					color: #A3A3A3;
					line-height: 40rpx;
					.active {
						color: #333;
						font-weight: 700;
					}
					.bar-item {
						padding: 28rpx 50rpx;
					}
				}
				.list-item {
					.item {
						padding: 30rpx 0;
						border-bottom: 1px solid #F6F6F6;
						display: flex;
						.item-info {
							margin-left: 20rpx;
							.info-head {
								display: flex;
								font-size: 28rpx;
								font-weight: 700;
								color: #333333;
								line-height: 40rpx;
								.head-gray {
									font-weight: 400;
									color: #A3A3A3;
									margin-left: 10rpx;
								}
							}
							.item-time {
								font-size: 24rpx;
								font-weight: 400;
								color: #A3A3A3;
								line-height: 34rpx;
								margin: 10rpx 0;
							}
							.item-content {
								height: 80rpx;
								background: #F6F6F6;
								border-radius: 12rpx;
								padding: 0 24rpx;
								display: flex;
								align-items: center;
								.content-text {
									font-size: 28rpx;
									font-weight: 600;
									color: #333333;
									line-height: 40rpx;
									margin-left: 10rpx;
									overflow:hidden;
									text-overflow:ellipsis;
									white-space:nowrap;
									max-width: 484rpx;
								}
							}
						}
					}
				}
				.pet-item {
					.item {
						padding: 42rpx 0;
						border-bottom: 1px solid #F6F6F6;
						display: flex;
						align-items: center;
						.item-info {
							margin-left: 20rpx;
							height: 104rpx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							flex: 5;
							.info-nickname {
								font-size: 28rpx;
								font-weight: 700;
								color: #333333;
								line-height: 40rpx;
							}
							.info-base {
								display: flex;
								margin-left: 40rpx;
								.base-sex {
									width: 36rpx;
									height: 36rpx;
									background: rgba(0, 149, 255, 0.08);
									border-radius: 2px;
									display: flex;
									justify-content: space-evenly;
								}
								.base-variety {
									height: 36rpx;
									background: rgba(255, 154, 0, 0.08);
									border-radius: 4rpx;
									font-size: 24rpx;
									font-weight: 400;
									color: #FF9A00;
									line-height: 32rpx;
									display: flex;
									align-items: cneter;
									justify-content: center;
									padding: 2rpx 18rpx;
									margin: 0 18rpx;
								}
								.base-age {
									height: 36rpx;
									background: rgba(131, 86, 243, 0.08);
									border-radius: 4rpx;
									font-size: 24rpx;
									font-weight: 400;
									color: #8356F3;
									line-height: 32rpx;
									display: flex;
									align-items: cneter;
									justify-content: center;
									padding: 2rpx 18rpx;
								}
							}
							.info-sign {
								font-size: 28rpx;
								font-weight: 400;
								color: #999999;
								line-height: 40rpx;
								overflow:hidden;
								text-overflow:ellipsis;
								white-space:nowrap;
								width: 492rpx;
							}
						}
						.item-icon {
							margin: 0 20rpx;
						}
					}
				}
				.adopt-empty {
					display: flex;
					flex-direction: column;
					align-items: center;
					.empty-text {
						font-size: 32rpx;
						font-weight: 500;
						color: #333333;
						line-height: 44rpx;
						letter-spacing: 2rpx;
						padding-top: 60rpx;
					}
				}
			}
		}
	}
</style>
